package com.mm.dss.demo;

import android.app.Dialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.library.BaseRecyclerAdapter;
import com.github.library.BaseViewHolder;
import com.google.gson.Gson;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mm.dss.demo.Interface.OkGoUtilsInterFace;
import com.mm.dss.demo.base.BaseActivity;
import com.mm.dss.demo.bean.TongZhiBean;
import com.mm.dss.demo.bean.XxCjBean;
import com.mm.dss.demo.tools.Constances;
import com.mm.dss.demo.tools.OkgoUtils;
import com.mm.dss.demo.tools.SharedPreferencedUtils;
import com.mm.dss.demo.tools.SwipeRefreshHelper;
import com.mm.dss.demo.tools.WebServiceUtils;
import com.mm.dss.demo.tools.WeiboDialogUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XxCjlistActivity extends BaseActivity {
    TextView txt_title;
    RelativeLayout layout_back;
    RecyclerView recyclerView;//
    private  TextView right;
    BaseRecyclerAdapter<XxCjBean> baseRecyclerAdapter;
    List<XxCjBean> tongZhiBeanlist=new ArrayList<>();
    private Dialog mWeiboDialog;
    private SwipeRefreshLayout swipregresh;

    private void setnet(boolean IsDialog) {
        tongZhiBeanlist.clear();
        if (IsDialog) {
            mWeiboDialog = WeiboDialogUtils.
                    createLoadingDialog(XxCjlistActivity.this,
                            "加载中...");
        }
        Map<String,String> map=new HashMap();
        map.put("xcry",SharedPreferencedUtils.getString(XxCjlistActivity.this,"username"));
        WebServiceUtils.callWebService(Constances.webhosturl, "N08_GetMessList", map, new WebServiceUtils.WebServiceCallBack() {
            @Override
            public void callBack(SoapObject result) {
                Log.i("信息采集列表接口",result.getProperty(0).toString());
                if (IsDialog) {
                    WeiboDialogUtils.closeDialog(mWeiboDialog);
                }
                finishRefresh();
                Gson gson=new Gson();
                //请求成功
                try {
                    JSONArray array=new JSONArray(result.getProperty(0).toString());
                    for (int i=0;i<array.length();i++){
                        XxCjBean xxcjBean=gson.fromJson(array.optJSONObject(i).toString(),XxCjBean.class);
                        tongZhiBeanlist.add(xxcjBean);

                    }
                    baseRecyclerAdapter.setData(tongZhiBeanlist);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xx_cjlist);
        txt_title= (TextView) findViewById(R.id.txt_title);
        layout_back= (RelativeLayout) findViewById(R.id.layout_back);
        recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
        right= (TextView) findViewById(R.id.right);
        right.setVisibility(View.VISIBLE);
        right.setText("信息上报");
        txt_title.setText("信息采集");
        layout_back.setVisibility(View.VISIBLE);
        swipregresh= (SwipeRefreshLayout) findViewById(R.id.swipregresh);
        initdata();
        initSwipeRefresh();
    }

    private void initdata() {

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        baseRecyclerAdapter=new BaseRecyclerAdapter<XxCjBean>
                (this,null,R.layout.item_tongzhiview) {
            @Override
            protected void convert(final BaseViewHolder helper, final XxCjBean item) {
                helper.setText(R.id.txt_tongzhi,item.getKSMC());
                helper.setText(R.id.time,item.getXCRQ());
                helper.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(XxCjlistActivity.this,XxCjinfoActivity.class);
                        intent.putExtra("jcd",item.getKSMC());
                        intent.putExtra("xcxx",item.getCJWT());
                        intent.putExtra("tp",item.getIMG()+"");
                        intent.putExtra("time",item.getXCRQ());
                        intent.putExtra("cjr",item.getXCRY());
                        intent.putExtra("rusername","真实姓名");
                        startActivity(intent);
                    }
                });
            }
        };
        baseRecyclerAdapter.openLoadAnimation(false);
        recyclerView.setAdapter(baseRecyclerAdapter);

    }

    @Override
    protected void onResume() {
        super.onResume();
        setnet(true);
    }

    public void onclick(View view){
        switch (view.getId()){
            case R.id.layout_back:
                finish();
                break;
            case R.id.right:
                startActivity(new Intent(XxCjlistActivity.this,XxcjActivity.class));
                break;
        }
    }

    /**
     * 初始化下拉刷新
     */
    private void initSwipeRefresh() {
        if (swipregresh != null) {
            SwipeRefreshHelper.init(swipregresh, new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    setnet(false);

                }
            });
        }
    }

    /**
     * 关闭刷新
     */
    public void finishRefresh() {
        if (swipregresh != null) {
            swipregresh.setRefreshing(false);
        }
    }
}
